﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using System.IO;
using System.Text.RegularExpressions;
using System.Management;
using System.Diagnostics;

namespace IPChangeApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void BtnGet_Click(object sender, EventArgs e)
        {
            Get();
        }

        protected static System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer();
        protected static List<NetworkInterface> lstNI = new List<NetworkInterface>();
        private void Form1_Load(object sender, EventArgs e)
        {
            CboNetWork.Items.Clear();            
            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            int len = nics.Length;
            for (int j = 0; j < len; j++)
            {
                NetworkInterface ni = nics[j];
                Console.WriteLine(jss.Serialize(ni));
                //if (ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
                {
                    lstNI.Add(ni);
                    CboNetWork.Items.Add(ni.Name);
                }
            }
            CboNetWork.SelectedIndex = 0;
            //Get();
        }


        void InitIPConfig(NetworkInterface nk)
        {
               if (nk.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
                {
                    IPInterfaceProperties property = nk.GetIPProperties();
                    foreach (UnicastIPAddressInformation ipInfo in property.UnicastAddresses)
                    {
                        if (ipInfo.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        {
                            txtIP.Text = ipInfo.Address.ToString();
                            txtSubMark.Text = ipInfo.IPv4Mask.ToString();
                        }
                    }
                    txtGateWay.Text = property.GatewayAddresses[0].Address.ToString();
                    txtDNS1.Text = property.DnsAddresses[0].ToString();
                    txtDNS2.Text = property.DnsAddresses[1].ToString();
            }
            else
            {

            }
        }


        void Get()
        {
            string name = CboNetWork.SelectedItem.ToString();
            NetworkInterface net = lstNI.Find(t => t.Name == name);
            if (net!=null)
            {
                InitIPConfig(net);
            }
        }

        private void BtnSet_Click(object sender, EventArgs e)
        {
            try
            {
                string[] ip = new string[] {txtIP.Text.Trim() };
                string[] SubMark = new string[] {txtSubMark.Text.Trim() };
                string[] GateWay = new string[] {txtGateWay.Text.Trim() };
                string[] DNS = new string[] {txtDNS1.Text.Trim(),txtDNS2.Text.Trim() };
                SetIpInfo(ip, SubMark, GateWay, DNS);
                MessageBox.Show("ip信息设置成功！","成功提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            catch(Exception er)
            {
                MessageBox.Show("设置出错："+er.Message,"出错提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }
        protected void SetIpInfo(string[] ip, string[] SubMark, string[] GateWay, string[] DNS)
        {
            ManagementBaseObject inPar = null;
            ManagementBaseObject outPar = null;
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                if (!(bool)mo["IPEnabled"]) continue;
                inPar = mo.GetMethodParameters("EnableStatic");
                inPar["IPAddress"] = ip;//ip地址
                inPar["SubnetMask"] = SubMark; //子网掩码
                mo.InvokeMethod("EnableStatic", inPar, null);//执行
                inPar = mo.GetMethodParameters("SetGateways");
                inPar["DefaultIPGateway"] = GateWay; //设置网关地址 1.网关;2.备用网关
                outPar = mo.InvokeMethod("SetGateways", inPar, null);//执行
                inPar = mo.GetMethodParameters("SetDNSServerSearchOrder");
                inPar["DNSServerSearchOrder"] = DNS; //设置DNS  1.DNS 2.备用DNS
                mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null);// 执行
                break; //只设置一张网卡，不能多张。
            }
        }
    }

}
